package rabbitmq.topics;

import com.rabbitmq.client.*;
import rabbitmq.constants.Constants;
import rabbitmq.utils.WorkUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = WorkUtils.createConnectionFactory();
        //1.建立连接
        Connection connection = factory.newConnection();
        //2.开启信道
        Channel channel = connection.createChannel();
        //3.声明队列
        channel.queueDeclare(Constants.TOPICS_QUEUE2,true,false,false,null);
        //4.接收消息
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body)
            {
                System.out.println("接收消息：" + new String(body));
            }
        };
        channel.basicConsume(Constants.TOPICS_QUEUE2,true, consumer);
        //5.释放资源
//        channel.close();
//        connection.close();
    }
}
